package com.hrt.demo.upload;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.hrt.util.upload.Plupload;
import com.hrt.util.upload.UploadUtil;

//@Controller
//@RequestMapping("/manage/demo/mutilUpload")
public class MutiFileUploadController2 {

	private List<File> files;

	@Autowired
	private UploadUtil uploadUtil;
	
	
	 public static final String FileDir = "uploadfile/";  
	
	@RequestMapping(value = "/show")
	public ModelAndView show() {

		ModelAndView model = new ModelAndView();

		model.setViewName("manage/demo/upload/mutilUpload/index");
		return model;
	}
	
	@RequestMapping(value = "/upload", method = RequestMethod.POST)
	public String upload(Plupload plupload,HttpServletRequest request, HttpServletResponse response)
	{
		//-1===6.jpg----1
		 System.out.println(plupload.getChunk() + "===" + plupload.getName() + "---" + plupload.getChunks());  
        
        plupload.setRequest(request);  
        //文件存储路径  
        File dir = new File(plupload.getRequest().getSession().getServletContext().getRealPath("/") + FileDir);  
          
          
        try {  
            //上传文件  
        	File file = uploadUtil.upload(plupload, dir);  
            //判断文件是否上传成功（被分成块的文件是否全部上传完成）  
        	if (file != null) {
        		
        		//获取上传的图片名称
        		//全路径
//        		 String UPLOAD_SUCCESS = WebParams.DO_MAIN + request.getContextPath() + "/" + FileDir + file.getName();
        		 String UPLOAD_SUCCESS = "/" + FileDir + file.getName();
        		 System.out.println("sdjshd===>"+UPLOAD_SUCCESS);
        
        		
            }else{
            	return "fail";
            }
              
        } catch (IllegalStateException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
        return "success";  
    }  
	


	public List<File> getFiles() {
		return files;
	}


	public void setFiles(List<File> files) {
		this.files = files;
	}

}
